##################################
#-*- coding:utf8 -*-             #
from de import malware 
from util import page_extract

import re     

class malicious_element_check(object):
    def mal_or_not(self,link):
        if self.mal_url(link)==False:
            return False
        
    def mal_url(self,link):
        if malware.url_check(link.url_text.url)==False:
            print 'malicous=',link.url_text.url
            return True
        else:
            return False    
        
class script_src_http(object):
    def mal_or_not(self):
        return self.url_check.mal_or_not(self)
  
    def get_url_title(self):
        self.url_title=page_extract.page_util(self.title).link()
#        self.mal_title=''
#        try:
#            pattern='(script)(\s+src="*'+page_extract.regx_lib().http+'"*></script>)*'                                        
##            self.mal_title=re.search('(/title\s*>\s*<\s*script)(\s+src="*(http://)?((([\w-]+\.)+[\w-]+)(/[\w-]+)*/([\w-]+.[jJ][Ss]))"*></script>)*',self.title).groups()
#            self.mal_title=re.search(pattern,self.title).groups()
#            return
#        except:            
#            pass
        
    def get_url_text(self):
        self.url_text=page_extract.page_util(self.text).link()
#        
#        self.injection_url=""
#        try:            
#            textreg=re.search('/title\s*>\s*<\s*script\s+src="*(http://)?((([\w-]+\.)+([\w-]+))(/[\w-]+)*/([\w-]+.[jJ][Ss]))"*></script>',self.text).groups()
#            self.injection_url=textreg[0]+textreg[1]
#            return
#        except Exception,ex:
#            pass
#        
#        try:
#            textreg=re.search('src="*(http://)?((([\w-]+\.)+([\w-]+))(/[\w-]+)*/([\w-]+.[jJ][Ss]))',self.text).groups()
#            self.injection_url=textreg[0]+textreg[1]
#            return
#        except Exception,ex:                
#            pass    
#        
#        try:
#            PE=page_util(self.text)
#            self.injection_url=PE.link().url
#        except Exception,ex:                
#            pass     

    def __init__(self,element):
        self.title=element.title
        self.href=element.href
        self.text=element.text
        self.get_url_text()
        self.get_url_title()
        self.url_check=malicious_element_check();
          
def handle(element):
    x=script_src_http(element)
    x.mal_or_not()
          
import key
def search_key(keyword): 
    return     key.key(keyword,handle)  
    
    
     
        